#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
#include <QPushButton>
#include <QDebug>
#include <QPainter>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 创建图标
    QIcon iconT(":/image/target.png");
    QIcon iconU(":/image/up.png");
    QIcon iconD(":/image/down.png");
    QIcon iconL(":/image/left.png");
    QIcon iconR(":/image/right.png");

    // 设置图标
    ui->target->setIcon(iconT);
    ui->up->setIcon(iconU);
    ui->down->setIcon(iconD);
    ui->left->setIcon(iconL);
    ui->right->setIcon(iconR);

    // 设置图标大小：
    ui->target->setIconSize(QSize(70,70));
    ui->up->setIconSize(QSize(40,40));
    ui->down->setIconSize(QSize(50,50));
    ui->left->setIconSize(QSize(40,40));
    ui->right->setIconSize(QSize(40,40));

    // 设置连续触发
    ui->up->setAutoRepeat(true);
    ui->down->setAutoRepeat(true);
    ui->left->setAutoRepeat(true);
    ui->right->setAutoRepeat(true);

    // 设置快捷键
    ui->up->setShortcut(QKeySequence(Qt::Key_W));
    ui->down->setShortcut(QKeySequence(Qt::Key_S));
    ui->left->setShortcut(QKeySequence(Qt::Key_A));
    ui->right->setShortcut(QKeySequence(Qt::Key_D));




}
Widget::~Widget()
{
    delete ui;
}

void Widget::opacity(QRect& rect)
{
    int x = rect.x(), y = rect.y();
    QPainter painter(ui->up);
   if(x>=130 && x <= 460 && y >= 320 && y <= 519)
   {
      ui->up->setFlat(true);
      ui->up->setStyleSheet("QPushButton{border:none;background:transparent;}");
   }
}

void Widget::on_up_clicked()
{
    QRect rect =  ui->target->geometry();
    int y = rect.y();
    int h = rect.height();
    opacity(rect);
    if(rect.y() <= 0){
        rect.setY(0);
    }
    else
    {
        rect.setY(y-10);
    }


    ui->target->setGeometry(rect.x(), rect.y(), rect.width(), h);
    qDebug() << rect.x() << " " << rect.y();
}

void Widget::on_right_clicked()
{
    QRect rect =  ui->target->geometry();
    int x = rect.x();
    int w = rect.width();
    int g = this->geometry().width()-w;

    if(rect.x() >= g){
        rect.setX(g);
    }
    else
    {
        rect.setX(x+10);
    }
    ui->target->setGeometry(rect.x(), rect.y(), w, rect.height());
    qDebug() << rect.x() << " " << rect.y();
}

void Widget::on_left_clicked()
{
    QRect rect =  ui->target->geometry();
    int x = rect.x();
    int w = rect.width();
    if(rect.x() <= 0){
        rect.setX(0);
    }
    else
    {
        rect.setX(x-10);
    }
    ui->target->setGeometry(rect.x(), rect.y(), w, rect.height());
    qDebug() << rect.x() << " " << rect.y();
}

void Widget::on_down_clicked()
{
    QRect rect =  ui->target->geometry();
    int y = rect.y();
    int h = rect.height();
    int g = this->geometry().height()-h;

    if(rect.y() >= g){
        rect.setY(g);
    }
    else
    {
        rect.setY(y+10);
    }
    ui->target->setGeometry(rect.x(), rect.y(), rect.width(), h);
    qDebug() << rect.x() << " " << rect.y();
}
